<?php
/**
 * 
 * @author Youssef Driouach
 *
 * Build a character string from the request form
 * 
 */
class Application_Model_Builder_Request
{
	protected $_form;
	
	public function __construct(array $options = null)
	{
		if (is_array($options)) {
			$this->setOptions($options);
		}
	}
	
	public function __set($name, $value)
	{
		$method = 'set' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid Address property');
		}
		$this->$method($value);
	}
	
	public function __get($name)
	{
		$method = 'get' . $name;
		if (('mapper' == $name) || !method_exists($this, $method)) {
			throw new Exception('Invalid address property');
		}
		return $this->$method();
	}
	
	/**
	 * create the request string character
	 * 
	 * @return string
	 */
	public function create()
	{
		$method = $this->getObjectselect().'Options';
		return $this->$method();
	}
	
	/**
	 * create the Application_Model_Request from the form data
	 * 
	 * @return Application_Model_Request 
	 */
	public function createRequest()
	{
		$request = new Application_Model_Request();
		$request->setClass($this->getObjectselect());
		$request->setOfferClass($this->getOfferclass());
		$request->setKeyword($this->getKeyword());
		$request->setAdmin_area_lvl1($this->getAdmin_area_lvl1());
		$request->setAdmin_area_lvl2($this->getAdmin_area_lvl2());
		$request->setCountry($this->getCountry());
		$request->setOptionss($this->create());
		$request->setPrice_interval($this->price_interval());
		$request->setDate(date('Y-m-d H:i:s'));
		return $request;
	}
	/**
	 * return the price interval brut without the 0.
	 * @return string
	 */
	public function price_interval()
	{
		return $this->getPricemin().":".$this->getPricemax();
	}
	
	
	public function objectOptions()
	{
		//.($this->getLocalisation() ? " AND department:".$this->getLocalisation() : "")
		return $this->getPrice_interval().$this->getAdmin_area_lvl1_req().$this->getAdmin_area_lvl2_req().$this->getCountry_req();
	}
	public function carOptions()
	{
		return $this->objectOptions().$this->getYear_interval().$this->getEnergy().$this->getTransmission().$this->getMileage_interval();
	}
	
	public function motorcycleOptions()
	{
		return $this->objectOptions().$this->getYear_interval().$this->getCylinder_interval().$this->getMileage_interval();
	}
	
	public function apartmentOptions()
	{
		return $this->objectOptions().$this->getSurface_interval();
	}
	
	public function houseOptions()
	{
		return $this->objectOptions().$this->getSurface_interval();
	}
	
	public function phoneOptions()
	{
		return $this->objectOptions();
	}
	
	public function desktopOptions()
	{
		return $this->objectOptions();
	}
	
	public function laptopOptions()
	{
		return $this->objectOptions();
	}
	
	public function tvOptions()
	{
		return $this->objectOptions();
	}
	
	public function clothingOptions()
	{
		return $this->objectOptions();
	}
	
	public function bookOptions()
	{
		return $this->objectOptions();
	}
	
	public function otherOptions()
	{
		return $this->objectOptions();
	}
	/**
	 * @return the $_form
	 */
	public function getForm() {
		return $this->_form;
	}

	/**
	 * @param field_type $_form
	 */
	public function setForm(Application_Form_Search $_form) {
		$this->_form = $_form;
	}

	/**
	 * get the offer class
	 * @return Ambigous <mixed, NULL, multitype:, multitype:unknown >
	 */
	public function getOfferclass(){
		return $this->_form->getValue('class');
	}
	
	public function getAdmin_area_lvl1(){
		return $this->_form->getValue('admin_area_lvl1');
	}
	
	public function getAdmin_area_lvl2(){
		return $this->_form->getValue('admin_area_lvl2');
	}
	
	public function getCountry(){
		return $this->_form->getValue('country');
	}
	
	public function getKeyword() {
		return $this->_form->getValue('keyword');
	}
	
	public function getPricemin() {
		return $this->_form->getValue('pricemin');
	}
	
	public function getPricemax() {
		return $this->_form->getValue('pricemax');
	}
	
	public function getSurfacemin() {
		return $this->_form->getValue('surfacemin');
	}
	
	public function getSurfacemax() {
		return $this->_form->getValue('surfacemax');
	}
	
	public function getCylindermin() {
		return $this->_form->getValue('cylindermin');
	}
	
	public function getCylindermax() {
		return $this->_form->getValue('cylindermax');
	}
	
	public function getYearmin() {
		return $this->_form->getValue('yearmin');
	}

	public function getYearmax() {
		return $this->_form->getValue('yearmax');
	}	
	
	public function getEnergy() {
		return $this->_form->getValue('energy');
	}
	
	public function getTransmission() {
		return $this->_form->getValue('transmission');
	}
	
	public function getMileagemin() {
		return $this->_form->getValue('mileagemin');
	}
	
	public function getMileagemax() {
		return $this->_form->getValue('mileagemax');
	}
	
	public function getObjectselect() {
		return $this->_form->getValue('objectselect');
	}
	
	public function getAdmin_area_lvl1_req(){
		if($this->getAdmin_area_lvl1() == "")
			return "";
		else
			return " AND admin_area_lvl1:".$this->getAdmin_area_lvl1();
	}
	
	public function getAdmin_area_lvl2_req(){
		if($this->getAdmin_area_lvl2() == "")
			return "";
		else
			return " OR admin_area_lvl2:".$this->getAdmin_area_lvl2();
	}
	
	public function getCountry_req(){
		if($this->getCountry() == "")
			return "";
		else
			return " AND coutry:".$this->getCountry();
	}
	
	public function getOfferClass_req(){
		if($this->getOfferclass() == "")
			return "";
		else
			return " AND offerClass:".$this->getOfferclass();
	}
	
	public function getEnergy_req(){
		if($this->getEnergy() == 0)
			return "";
		else return " AND energy:".$this->getEnergy();
	}
	
	public function egtTransmission_req(){
		if($this->getTransmission() == 0)
			return "";
		else 
			return " AND transmission:".$this->getTransmission();
	}
	public function getMileage_interval() {
		if($this->getMileagemin() == 0 && $this->getMileagemax() == 0)
			return "";
		if($this->getMileagemin() != 0 && ($this->getMileagemax() == 0 || $this->getMileagemax() == ""))
			return " AND mileage:[".$this->adjustnumber(6, $this->getMileagemin(), '0')." TO 999999]";
		else
			return " AND mileage:[".$this->adjustnumber(6, $this->getMileagemin(), '0')." TO ".$this->adjustnumber(6, $this->getMileagemax(), '0')."]";
	}
	
	public function getPrice_interval() {
		if($this->getPricemin() == 0 && $this->getPricemax() == 0)
			return "";
		else 
			return " AND price:[".$this->adjustfloat(9, $this->getPricemin(), '0')." TO ".$this->adjustfloat(9, $this->getPricemax(), '0')."]";
	}
	
	public function getSurface_interval() {
		if($this->getSurfacemin() == 0 &&  $this->getSurfacemax() == 0)
			return "";
		if($this->getSurfacemin() != 0 &&  $this->getSurfacemax() == 0)
			return " AND surface:[".$this->adjustnumber(4, $this->getSurfacemin(), '0')." TO 9999]";
		else 
			return " AND surface:[".$this->adjustnumber(4, $this->getSurfacemin(), '0')." TO ".$this->adjustnumber(4, $this->getSurfacemax(), '0')."]";
	}
	
	public function getCylinder_interval() {
		if($this->getCylindermin() == 0 &&  $this->getCylindermax() == 0)
			return "";
		if($this->getCylindermin() != 0 &&  $this->getCylindermax() == 0)
			return " AND cylinder:[".$this->adjustnumber(5, $this->getCylindermin(), '0')." TO 99999]";
		else 
			return " AND cylinder:[".$this->adjustnumber(5, $this->getCylindermin(), '0')." TO ".$this->adjustnumber(5, $this->getCylindermax(), '0')."]";
	}
	
	public function getYear_interval() {
		if($this->getYearmin() == 0 &&  $this->getYearmax() == 0)
			return "";
		if($this->getYearmin() != 0 &&  $this->getYearmax() == 0)
			return " AND year:[".$this->getYearmin()." TO ".date('Y')."]";
		else 
			return " AND year:[".$this->getYearmin()." TO ".$this->getYearmax()."]";
	}
	
	/**
	 * this function adjust number 
	 * 
	 * @example adjustnumber(4,'28','0') returns '0028'
	 * @example adjustnumber(3,'2561','0') returns '2561'
	 * @param int $len the number of character 
	 * @param for price $len must be 9
	 * @param for mileage $len must be 6
	 * @param for cylinder $len must be 5
	 * @param for surface $len must be 4
	 * @param string $number
	 * @param string $char the character with which we adjusted
	 * @return string
	 */
	public function adjustnumber($len,$number,$char)
	{
		$numberLen = strlen($number);
		$dif = $len - $numberLen;
		$pre = '';
		while($dif > 0)
		{
			$pre = $pre.$char;
			$dif = $dif - 1;
		}
		
		return $pre.$number;
	}
	
	public function adjustfloat($len,$number,$char){
		$farray = explode(".",$number);
		if(count($farray) == 2){
			return $this->adjustnumber($len, $farray[0], $char).$farray[1];
		}
		else{
			return $this->adjustnumber($len, $farray[0], $char).'00';
		}
	}
	
}